package com.woniuxy.dr_mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.dr_mall.entity.DrOrderItemView;
import com.woniuxy.dr_mall.entity.DrOrders;
import com.woniuxy.dr_mall.entity.DrProducts;
import org.apache.ibatis.annotations.*;
import com.woniuxy.dr_mall.entity.DrSpecPrice;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
public interface DrOrdersMapper extends BaseMapper<DrOrders> {

    /**
     * 根据用户id查询下单过的商品信息
     *
     * @param userId 用户id
     * @return 购买过的商品信息
     * @author yy
     * @since 2021-8-14 16:44
     */
    @Select("select * from v_buyproduct where userid=#{userid}")
    List<DrProducts> getOrdersByUserId(int userId);

    //通过用户id，查询订单信息 2021-8-16 叶官鑫
    //加了一个isDel作为查询条件查询
    @Select("select * from dr_orders where userId=#{userId} and isDel=#{isDel}")
    List<DrOrders> userIdSelectOrders(@Param("userId") int userId,@Param("isDel") String isDel);


    @Select("select * from dr_orders where status='1' and isDel='n'")
    @Results({
            @Result(column = "userid", property = "userId"),
            @Result(column = "userid", property = "user", one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrUserMapper.selectImformationById"
            ))
    })
    List<DrOrders> getAll();


    //通过用户id和订单id删除订单信息 叶官鑫 2021-8-17
    @Update("update dr_orders set isDel='n' where userId =#{userId} and id=#{orderId}")
    void idDeleteOrder(@Param("userId")int userId,@Param("orderId") int orderId);


    //通过订单id查询订单的详细信息，以及用户账号 叶官鑫 2021-8-17
    @Select("select du.account,do.* from dr_orders do,dr_user du where do.userid=du.id and do.id=#{orderId};")
    DrOrders selectOrderByid(int orderId);


    //通过orderId（商品id）查询，查询商品的详细信息  叶官鑫 2021-8-17
    @Select("select * from dr_orderitem_view where orderid=#{orderId}")
    @Results({
            @Result(column = "specpriceid" ,property = "specpriceId"),
            @Result(column = "productid" ,property = "productId"),
            @Result(property = "drSpecPrice",column = "specpriceid",one =@One( fetchType = FetchType.LAZY,select = "com.woniuxy.dr_mall.mapper.DrSpecPriceMapper.selectSpecPriceImformation")),
            @Result(property = "productName",column = "productId" ,one =@One( fetchType = FetchType.LAZY,select = "com.woniuxy.dr_mall.mapper.DrProductsMapper.selectProductByid")),
            @Result(property = "drPicture",column = "productId" ,many = @Many(fetchType = FetchType.LAZY,select = "com.woniuxy.dr_mall.mapper.DrPictureMapper.selectProductsPicture"))
    })
    List<DrOrderItemView> selectProductsImformationById(int orderId);


    @Update("update dr_orders set status='2' where id=#{id}")
    void forbidById(@Param("id") int id);

    @Select("select count(*) from dr_orders where status = '1' and userId = #{userId}")
    int getCountByUserId(@Param("userId")Integer userId);
}


